#include <stdio.h>  
#include <stdlib.h>  
#include <pthread.h>  
#include <unistd.h>  
  
pthread_mutex_t mutex;  
int a = 100;  
void *fun(void *arg)  
{  
    while (a)  
    {  
        pthread_mutex_lock(&mutex);  
        printf("%d\n", a);  
        pthread_mutex_unlock(&mutex);  
           sleep(1);
    }  
    return NULL;  
}  
  
int main(int argc, char *argv[])  
{  
    pthread_t thread;  
    pthread_mutex_init(&mutex, NULL);  
    int ret = pthread_create(&thread, NULL, fun, NULL);  
    for (int i = 0; i < 10; i++)  
    {  
       
        pthread_mutex_lock(&mutex);  
        a-=10;  
       pthread_mutex_unlock(&mutex);  
        sleep(1);
    }  
  
    pthread_join(thread, NULL);  // 等待线程结束  
    pthread_mutex_destroy(&mutex);  // 销毁互斥锁  
  
    return 0;  
}